package com.hgxd.mc.controller;

import com.hgxd.mc.collection.StiffNessDataGet;
import com.hgxd.mc.entity.StiffNessData;
import com.hgxd.mc.service.StiffNessService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

import static com.hgxd.mc.collection.StiffNessDataGet.analysisReadRespond;

@RestController
@RequestMapping("/write")
public class StiffNessController {

    @Resource
    private StiffNessService stiffNessService;

    @GetMapping("/stiffness")
    public void StiffNessWrite() {

        StiffNessDataGet getStiffNess = new StiffNessDataGet("192.168.1.10", 5000);

        if (getStiffNess.connect()) {
            byte[] address = new byte[]{0x00, 0x10, 0x00};
            byte[] number = new byte[]{0x00, 0x01};
            byte tab = (byte) 0xB4;

            String respondMess = null;
            try {
                respondMess = getStiffNess.sendMcReadRequest(address, tab, number);
            } catch (IOException e) {
                e.printStackTrace();
            }

            if(respondMess != null){
                List<List<String>> resultStrLists = analysisReadRespond(respondMess);
                StiffNessData stiffNessData = new StiffNessData();

                //写入数据库
                stiffNessData = getStiffNess.getValues(resultStrLists, stiffNessData);
                stiffNessService.save(stiffNessData);
            }

//            Thread.sleep(600);
            getStiffNess.disconnect();
        } else {
            System.out.println("连接失败！！！");
        }

    }

}
